import { reactive, markRaw } from 'vue';
import { removeUndefinedParams } from '@/utils/index';
import { CTableProps } from '@/components/CTable';
import { BasicPagingListParams } from '@/apis/basicModel';
import type { SearchOptionsType, TableFieldsSettingType } from '@/components/CPageTable';
import { ColumnSettingTypeEnum } from '@/components/CPageTable';
import { getCurrentOrganizationList } from '@/apis/common/common';
import { getTaskList } from '@/apis/eci/emissionAccounting/inventoryTask';
import { AccountStatusEnum, InventoryTaskListAccountStatusOption } from '@/constants/enum';

export const useTaskList = () => {
  const searchForm = reactive({
    organizationCode: undefined,
    inventoryYear: undefined,
    createTime: [] as string[],
    accountStatus: undefined as unknown as keyof typeof AccountStatusEnum
  });
  const searchOptions: SearchOptionsType[] = markRaw([
    {
      name: '组织名称',
      placeholder: '组织名称',
      field: 'organizationCode',
      type: 'select',
      remoteMethod: (query: string): Promise<any> => {
        const params = {
          organizationName: query
        };
        return getCurrentOrganizationList(params).then(res => {
          const ans = res.map(item => {
            return {
              label: item.organizationName,
              value: item.organizationCode
            };
          });
          return ans;
        });
      },
      style: 'width: 192px'
    },
    {
      name: '盘查周期',
      field: 'inventoryYear',
      type: 'datePicker',
      dateType: 'year',
      format: 'YYYY'
    },
    {
      name: '创建时间',
      field: 'createTime',
      type: 'datePicker',
      dateType: 'datetimerange',
      startPlaceholder: '创建起始时间',
      endPlaceholder: '创建截止时间',
      format: 'YYYY-MM-DD HH:mm:ss'
    },
    {
      name: '核算状态',
      placeholder: '核算状态',
      field: 'accountStatus',
      type: 'select',
      options: InventoryTaskListAccountStatusOption
    }
  ]);

  // 表格配置
  const tableOptions: CTableProps = {
    loadData
  };

  function loadData(parameter: BasicPagingListParams) {
    const params = Object.assign(handleParams(), parameter);
    return getTaskList(params);
  }

  // 处理参数
  const handleParams = () => {
    const [createTimeBegin, createTimeEnd] = searchForm.createTime || [];
    // 参数
    let params = {
      organizationCode: searchForm?.organizationCode,
      inventoryYear: searchForm?.inventoryYear,
      accountStatus: searchForm?.accountStatus,
      createTimeBegin,
      createTimeEnd
    };
    params = removeUndefinedParams(params);
    return params;
  };

  const tableFieldsSetting: TableFieldsSettingType[] = [
    {
      title: '组织名称/任务编号',
      field: 'organizationName',
      slot: 'organizationNameSlot',
      minWidth: 200,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '盘查周期',
      field: 'inventoryYear',
      minWidth: 120,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '创建人/创建时间',
      field: 'creator',
      slot: 'creatorSlot',
      width: 180,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      key: 'accountProgress',
      title: '核算进度',
      field: 'accountProgress',
      slot: 'accountProgressSlot',
      headerSlot: 'accountProgressHeaderSlot',
      width: 120,
      align: 'right',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      key: 'accountStatus',
      title: '核算状态',
      field: 'accountStatus',
      slot: 'accountStatusSlot',
      width: 100,
      fixed: 'right',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '操作',
      field: 'op',
      slot: 'op',
      width: 155,
      fixed: 'right',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.FIXED
      }
    }
  ];

  return {
    searchForm,
    searchOptions,
    tableOptions,
    tableFieldsSetting
  };
};
